package com.tpc.server.systeme.service;

/**
 * @version 0.0.1
 * @author frederic.fortin
 * @modified 2010-08-17
 */

import java.sql.ResultSet;
import java.util.logging.Logger;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.tpc.client.systeme.core.service.TagService;
import com.tpc.client.systeme.core.error.TPCauthentificationException;
import com.tpc.dao.extend.Tag;
import com.tpc.server.systeme.utils.SecuriteManager;
import java.sql.SQLException;


@SuppressWarnings("serial")
public class TagServiceImpl extends RemoteServiceServlet implements TagService {

    private static final long serialVersionUID = 3L;
    private final static Logger objLog = Logger.getLogger(TagServiceImpl.class.getName());
    
    public TagServiceImpl() {

    }
    
    /** LoadListe **/
    public Tag[] loadList(String tagGroupId) {
        try {
            Tag[] tags = null;
            SecuriteManager objSecurite = new SecuriteManager(this.getThreadLocalRequest());
            if (objSecurite.isAccessAllowed(Tag.class.getSimpleName(),"READ")) {
                String sql = "SELECT id,name FROM tag_key,taggroup_has_key WHERE tag_key.id = taggroup_has_key.tagkeyId AND taggroup_has_key.taggroupId = ? AND tag_key.life = ?";
                String[] params = {tagGroupId,objSecurite.getProperties().getProperty("ENABLE")};
                ResultSet rs = objSecurite.getDataBaseManager().executeQuery(sql,params);
                if (rs != null) {
                    int i = 0;
                    int intNbrRow = 0;
                    rs.last();
                    intNbrRow = rs.getRow();
                    tags = new Tag[intNbrRow];
                    rs.beforeFirst();
                    while (rs.next()) {
                        tags[i] = new Tag();
                        tags[i].setId(rs.getInt("id"));
                        tags[i].setName(rs.getString("name"));
                        i++;
                    }
                }
                objSecurite.getDataBaseManager().closeConnection();
            }
            return tags;
        }
        catch (Exception objx) {
            objLog.info("message 2: "+objx);
            return null;
        }
    }
    
}